
/*
 * Created on 7 juin 2005
 */
package communication.thirdLayer;
import java.util.*;

import objects.Station;
import communication.WishedEmission;
import communication.frequencyChoice.*;
import kernel.DataWishedEmission3;
import java.io.Serializable;
import utils.*;

/**
 * @author ttran
 * In the mode ad-hoc, all the communications that users wants to realize are recorded
 * under the forme of WishedEmission3.
 * In other modes, they are recorded under the forme of WishedEmission 
 * or WishedEmission2 (second level of communication)
 */
public class WishedEmission3 implements Serializable {
    /**
     * The station sender
     */
    Station sender;
    /**
     * Vector of all the Stations receivers
     */
    Vector<Station> receivers;

    /**
     * time to start the emission
     */	
    double start;


    /**
     * the length of the message to be sent
     */ 
    Vector<String> parameters;
	
    private float _frequency; // frequence sur laquelle souhaite communiquer le transmetteur
	
    private AlgoFreqChoice _algoFreq; // used algorithm for choosing the frequency of the transmission
	
    public WishedEmission3(Vector<Station> r,Station s, 
			   double t, Vector<String> l){
	receivers=r;
	sender=s;
	start=t;
	parameters=l;
	_algoFreq=new RandomFrequency();
	_frequency = _algoFreq.frequencyChoice(s,r);
    }

    public Station getSender(){
	return sender;
    }
	
    public Vector<Station> getReceivers(){
	return receivers;
    }
	
    public double getStartTime(){
	return start;
    }
	
    public Vector<String> getParameters(){
	return this.parameters;
    }

	
    /**
     * @return the frequency of the communication
     */

    public float getFrequency(){
	return _frequency;
    }
	
    /**
     * @param the new frequency
     */

    public void setFrequency(float f){
	_frequency = f;
    }
	
    /**
     * Add a wishedEmission3 into the vector v sorting in the order of starting time
     * @param v the vector containing WishedEmission3
     * @param dw3 the WishedEmission3 
     */
	

    public static boolean insertDataWishedEmission3(Vector v,DataWishedEmission3 dw3){
	System.out.println("hello from insertDataWishedEmission -> ajouter dans mobileFrame.wishedEmission3");
	boolean fait=false;
	if(v.size()==0){
	    v.add(dw3); 
	    fait=true;
	}else{
	    int i=0;
	    int j=0;
	    while(i<v.size() && (!fait)){
		DataWishedEmission3 temp=(DataWishedEmission3) v.get(i);
		if(temp.getStartTime()>dw3.getStartTime()){
		    j=i;
		    fait=true;
		}
		i++;
	    }
	    System.out.println("fait:"+fait+j);
	    if (fait) v.add(j,dw3);
	    else{ 
		v.add(dw3);
		fait=true;
	    }
	}
	return fait;
    }
    /**
     * Add a wishedEmission3 into the vector wishedEmission3 of the station sender
     * of this wishedEmission3. This vector is  sorted in the order of starting time
     * @return
     */
    public boolean insertWishedEmission3(){
	Vector v=this.getSender().getWishedEmission3();

	boolean fait=false;
	if(v.size()==0){ 
	    v.add(this); 
	    fait=true;
	}
	else{
	    int j=0;
	    for(int i=0;i<v.size();i++){

		WishedEmission3 temp=(WishedEmission3) v.get(i);
		if(temp.getStartTime()>=this.getStartTime()){
		    j=i;
		    fait=true;
		}
	    }
	    if (fait) v.add(j,this);
	    else{ 
		v.add(this);
		fait=true;
	    }
	}
	return fait;
    }	
	
    /**
     * Print a WishedEmission3
     */
    public String toString(){
	String s="[";
	for(int i=0; i<this.receivers.size();i++){
	    s+=((Station)this.receivers.get(i)).getName()+"; ";
	}
	s+="]";
	return "wishedEmission3 (\n" 
	    +"from " + this.sender + " to " + s
	    + "\n	          from date " + this.getStartTime() + " param "
	    + this.parameters + " )";
    }


    public String xmlDump(){
	HashMap<String,Object> attr = new HashMap<String,Object>();
	attr.put("start",getStartTime());
	attr.put("params",getParameters());
	attr.put("layer","3");
	String s = XMLHelper.XMLOpenTag("communication",attr);
	for(Station st : receivers){
	    HashMap<String,Object> a = new HashMap<String,Object>();
	    a.put("name",st.getName());
	    s = s + XMLHelper.XMLCompleteTag("destination",a);
	}
	s = s + XMLHelper.XMLCloseTag("communication");
	return s;	 
    }

}

